﻿using Newtonsoft.Json;
using SouthKoreaCoffee.CoffeeModel;
using SouthKoreaCoffee.IceModel;
using SouthKoreaCoffee.IOModel;
using SouthKoreaCoffee.Model;
using SouthKoreaCoffee.PrintModel;
using SouthKoreaCoffee.RobotModel;
using SouthKoreaCoffee.SocketClientsModel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static SouthKoreaCoffee.SocketClientsModel.CoffeeModels;
using static System.Net.Mime.MediaTypeNames;

namespace SouthKoreaCoffee
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static bool isMake = true;
        /// <summary>
        /// 基本配置存储位置
        /// </summary>
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"IPorPortMessage.txt";
        /// <summary>
        /// M12警告
        /// </summary>
        public static string WarnPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Warn";
        /// <summary>
        /// M12故障
        /// </summary>
        public static string BreakPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"BreakDowm";
        /// <summary>
        /// 正在制作
        /// </summary>
        public static List<CoffeeMakeModel> coffeeMaking { get; set; } = new List<CoffeeMakeModel>();
        public static int zsInit = 0;
        public void DYCX()
        {
            while (true)
            {
                try
                {
                    PwebSockets.Instance.DyCX();
                    Thread.Sleep(1000);
                    if (WuLiaoCoffeeSel.Instance.PrintBol)
                    {
                        ClientList clientList = new ClientList();
                        clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                        clientList.code = 2;
                        SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                        break;
                    }
                }
                catch (Exception)
                {
                    Thread.Sleep(1000);
                }

            }
        }
        public MainWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 全部订单查询
        /// </summary>
        public static void MessageShow()
        {
            while (true)
            {
                try
                {
                    Device device = new Device();
                    string lists = JsonConvert.SerializeObject(device);
                    var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);
                    if (data != null)
                    {
                        Console.WriteLine(data);
                        Console.WriteLine(isMake);
                        if (isMake)
                        {
                            var msglist = JsonConvert.DeserializeObject<HTTPMsgs>(data);
                            if (msglist != null)
                            {
                                if (msglist.data.ToString().Length > 10)
                                {
                                    var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                    if (list.Count > 0)
                                    {
                                        if (isMake)
                                        {
                                            if (list.Count > 0)
                                            {
                                                var ddd = list.FirstOrDefault(c => c.typeList2.Any(a => (a.status == "3" || a.status == "4") && !coffeeMaking.Any(b => b.OrderNum == c.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId)));
                                                if (ddd == null) continue;
                                                if (ddd.typeList2.Count > 0)
                                                {

                                                    var make = ddd.typeList2.FirstOrDefault(a => (a.status == "3" || a.status == "4") && !coffeeMaking.Any(b => b.OrderNum == ddd.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId));
                                                    if (make != null)
                                                    {
                                                        Console.WriteLine(make.num.ToString() + " " + DateTime.Now.ToString());
                                                        if (make.jsonCodeVal.Length > 4)
                                                        {
                                                            var coffeecode = JsonConvert.DeserializeObject<List<specification>>(make.jsonCodeVal);
                                                            if (coffeecode.Count > 0)
                                                            {
                                                                var coffeeID = coffeecode.FirstOrDefault(a => a.classCode != "");
                                                                if (coffeeID != null)
                                                                {
                                                                    bool robotL = RobotModelLeft.Instance.LRobotJQRYX_Sel();
                                                                    if (robotL)
                                                                    {
                                                                        //if (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)
                                                                        CoffeeMaking(coffeeID.classCode, make, ddd.orderNum);
                                                                        Console.WriteLine("一单完成！");
                                                                        // CoffeeMaking("5003", null, "5003");
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    //return;
                    Thread.Sleep(1000);
                }
            }
        }
        public static void CoffeeMaking(string makecoffeeName, DeviceOrderQueue list, string orderNum, bool CoffeeBol = true)
        {
            try
            {
                if (!isMake) return;
                isMake = false;
                int sugarCode = 0;//糖型
                int cupCode = 0;//杯型
                double sugerV = 1;//糖量
                int code = 0;
                var msgs = JsonConvert.DeserializeObject<List<specification>>(list.jsonCodeVal);
                Console.WriteLine(DateTime.Now.ToString() + "  cx10" + list.jsonCodeVal);
                if (msgs != null)
                {
                    if (msgs.Any(a1 => a1.SugarCode != ""))
                    {
                        sugarCode = msgs.Any(a1 => a1.SugarCode == "1") ? 1 : msgs.Any(a1 => a1.SugarCode == "2") ? 2 : msgs.Any(a1 => a1.SugarCode == "3") ? 3 : msgs.Any(a1 => a1.SugarCode == "4") ? 4 : msgs.Any(a1 => a1.SugarCode == "5") ? 5 : msgs.Any(a1 => a1.SugarCode == "6") ? 6 : 0;
                    }
                    if (msgs.Any(b => b.CupType != ""))
                    {
                        cupCode = msgs.Any(b => b.CupType == "1") ? 1 : msgs.Any(b => b.CupType == "2") ? 2 : msgs.Any(b => b.CupType == "0") ? 0 : 4;
                    }
                    if (msgs.Any(b => b.SyrupLevel != ""))
                    {
                        sugerV = msgs.Any(b => b.SyrupLevel == "1") ? 1 : msgs.Any(b => b.SyrupLevel == "2") ? 0.5 : msgs.Any(b => b.SyrupLevel == "3") ? 1.5 : 1;
                    }
                    if (msgs.Any(a => a.classCode != ""))
                    {
                        var cod = msgs.FirstOrDefault(a => a.classCode != "");
                        code = int.TryParse(cod.classCode, out int clas) ? clas : 5001;
                    }
                }
                if (!(WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol || WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol))
                {
                    isMake = true;
                    return;
                }
                while (true)
                {
                    EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                    status.orderGoodsId = list.id;
                    status.orderId = list.orderId;
                    status.status = 4;
                    string lists = JsonConvert.SerializeObject(status);
                    var ddd1 = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                    if (ddd1 != null)
                    {
                        if (ddd1 != "")
                        {
                            var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                            if (list10 != null)
                            {
                                if (list10.code == "0" || list10.code == "1")
                                {
                                    Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                    break;
                                }
                            }
                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                        }

                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(500);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 10;
                clientList.IsMake = false;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                int MidCode = code / 100;
                int CoffeeCode = code % 100;
                Thread.Sleep(200);
                if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                    RobotModelLeft.Instance.RobotQJB1_UP();
                else
                    RobotModelLeft.Instance.RobotQJB2_UP();
                Thread.Sleep(200);
                if (MidCode == 52)
                {
                    RobotModelLeft.Instance.RobotQJB_UP();
                    Thread.Sleep(200);
                }
                else if (MidCode == 51)
                {
                    RobotModelLeft.Instance.RobotQDY_UP();
                    Thread.Sleep(200);
                }
                if (code == 5001 || code == 5002 || code == 5214)
                {
                    RobotModelLeft.Instance.RobotWNKF_UP();
                    Thread.Sleep(200);
                }
                if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3 || sugarCode == 4 || sugarCode == 5 || sugarCode == 6)
                {
                    Thread.Sleep(200);
                    RobotModelLeft.Instance.RobotQJGT_UP();
                }
                Thread.Sleep(200);
                RobotModelLeft.Instance.RobotQJKF_UP();
                Thread.Sleep(200);
                RobotModelLeft.Instance.RobotKSZZ_UP();
                Thread.Sleep(500);
                CoffeeModels.Instance.QBMsg(cupCode);
                Thread.Sleep(200);
                while (true)
                {
                    while (!IOMsg.Instance.CFBol) { Thread.Sleep(10); }
                    Thread.Sleep(500);
                    if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol)
                    {
                        Thread.Sleep(100);
                        RobotModelLeft.Instance.RobotJBCG_UP();
                        Thread.Sleep(100);
                        if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3 || sugarCode == 4 || sugarCode == 5 || sugarCode == 6)
                        {
                            while (true)
                            {
                                bool RobotBol = false;
                                Thread.Sleep(200);
                                RobotBol = RobotModelLeft.Instance.LRobotDDGT_Sel();
                                Thread.Sleep(200);
                                if (RobotBol)
                                {
                                    Thread.Sleep(200);
                                    RobotModelLeft.Instance.RobotQJGT_Down();
                                    Thread.Sleep(200);
                                    break;
                                }
                            }
                            while (!IOMsg.Instance.CFBol) { Thread.Sleep(100); }
                            IOMsg.Instance.CFBol = false;
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            if (sugarCode == 1)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit1 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_end);
                            }
                            else if (sugarCode == 2)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit2 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_end);
                            }
                            else if (sugarCode == 3)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit3 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_end);
                            }
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            IOMsg.Instance.CFBol = true;
                            Thread.Sleep(1000);
                            RobotModelLeft.Instance.RobotGTWC_UP();
                            Thread.Sleep(200);
                        }
                        if (MidCode == 52)
                        {
                            try
                            {
                                while (true)
                                {
                                    bool RobotBol = false;
                                    Thread.Sleep(200);
                                    RobotBol = RobotModelLeft.Instance.LRobotDJBD_Sel();
                                    Thread.Sleep(200);
                                    if (RobotBol)
                                    {
                                        break;
                                    }
                                }
                                Thread.Sleep(200);
                                if (WuLiaoCoffeeSel.Instance.Ice1_Sel_Bol)
                                {
                                    IceMakingMsg1.Instance.AllBol = false;
                                    Thread.Sleep(1000);
                                    IceMakingSerialPort1.Instance.SerialSend(IceMakingModel.Instance.IceMaking);
                                    Thread.Sleep(1000);
                                    IceMakingMsg1.Instance.AllBol = true;
                                    Thread.Sleep(4000);
                                    for (int i = 0; i < 2; i++)
                                    {
                                        if (!WuLiaoCoffeeSel.Instance.Weigh1_Sel_Bol)
                                        {
                                            IceMakingMsg1.Instance.AllBol = false;
                                            Thread.Sleep(1000);
                                            IceMakingSerialPort1.Instance.SerialSend(IceMakingModel.Instance.IceMaking3);
                                            Thread.Sleep(1000);
                                            IceMakingMsg1.Instance.AllBol = true;
                                            Thread.Sleep(2000);
                                        }
                                    }
                                }
                                Thread.Sleep(200);
                                RobotModelLeft.Instance.RobotJBWC_UP();
                                Thread.Sleep(200);
                                RobotModelLeft.Instance.RobotQJB_Down();
                            }
                            catch (Exception)
                            {
                            }
                        }
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotJBCG_Down();
                        Thread.Sleep(200);
                        Console.WriteLine(DateTime.Now.ToString() + "  cx17");
                        Console.WriteLine(DateTime.Now.ToString() + "  cx18");
                        break;
                    }
                    else
                    {
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotJBSB_UP();
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotQJCJB_Down();
                        Thread.Sleep(200);
                        CoffeeModels.Instance.QBMsg(cupCode);
                    }
                }
                while (!CoffeeMsg.Instance.isMaking) { Thread.Sleep(10); }
                Thread.Sleep(200);
                while (true)
                {
                    bool RobotBol = false;
                    Thread.Sleep(200);
                    RobotBol = RobotModelLeft.Instance.LRobotDKF_Sel();
                    Thread.Sleep(200);
                    if (RobotBol)
                    {
                        RobotModelLeft.Instance.RobotGTWC_Down();
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotJBWC_Down();
                        break;
                    }
                }
                CoffeeMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                //CoffeeMakeNow(2);
                CoffeeMsg.Instance.CoffeeMakeNow(code, sugerV);
                Thread.Sleep(500);
                CoffeeMsg.Instance.AllBol = true;

                //while (isYC)
                //{
                //    Thread.Sleep(100);
                //}
                Thread.Sleep(1000);
                while (CoffeeMsg.Instance.isMaking && (CoffeeMsg.Instance.isBreakDown && CoffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                Thread.Sleep(1000);
                while (!CoffeeMsg.Instance.isMaking && (CoffeeMsg.Instance.isBreakDown && CoffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJKF_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotKFWC_UP();
                Thread.Sleep(200);
                if (MidCode == 51)
                {
                    try
                    {
                        var imgs = GetUrlToBase(list.lhImgPath);
                        SaveImage(imgs, "img.jpg");
                        SaveImg();
                    }
                    catch (Exception)
                    {
                    }
                    while (true)
                    {
                        bool RobotBol = false;
                        Thread.Sleep(200);
                        RobotBol = RobotModelLeft.Instance.LRobotDDY_Sel();
                        Thread.Sleep(200);
                        if (RobotBol)
                        {
                            RobotModelLeft.Instance.RobotQDY_Down();
                            break;
                        }

                    }
                    #region 新的打印机
                    PwebSockets.Instance.DyCS(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"print\img.png");
                    Thread.Sleep(200);
                    RobotModelLeft.Instance.RobotDYWC_UP();
                    #endregion
                }
                IOMsg.Instance.QK();
                GC.Collect();
                bool robotL1 = RobotModelLeft.Instance.LRobotJQRYX_Sel();
                Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                if (robotL1)
                {
                    if (WuLiaoCoffeeSel.Instance.RobotWarn != true)
                    {

                        WuLiaoCoffeeSel.Instance.RobotWarn = true;

                    }
                }
                else
                {
                    if (WuLiaoCoffeeSel.Instance.RobotWarn != false)
                    {
                        WuLiaoCoffeeSel.Instance.RobotWarn = false;

                    }
                }
                CoffeeMakeModel makeModel = new CoffeeMakeModel();
                makeModel.OrderGoodsId = list.id;
                makeModel.OrderId = list.orderId;
                makeModel.OrderNum = orderNum;
                coffeeMaking.Add(makeModel);
                ClientList clientList2 = new ClientList();
                clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList2.code = 2;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
                isMake = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        public static void SaveImg()
        {
            try
            {
                using (System.Drawing.Image originalImage = System.Drawing.Image.FromFile(@"print/img.jpg"))
                {
                    // 确定圆形的尺寸和位置
                    int width = 200; // 圆形宽度
                    int height = 200; // 圆形高度
                    int x = (originalImage.Width - width) / 2; // 圆形中心在图片中的x坐标
                    int y = (originalImage.Height - height) / 2; // 圆形中心在图片中的y坐标

                    // 创建一个新的Bitmap用于绘制圆形图像
                    using (Bitmap bmp = new Bitmap(width + 10, height + 10))
                    {
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            // 设置高质量插值法
                            g.SmoothingMode = SmoothingMode.HighQuality;
                            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            g.CompositingQuality = CompositingQuality.HighQuality;
                            g.Clear(System.Drawing.Color.White);
                            // 创建一个路径用于圆形
                            using (GraphicsPath path = new GraphicsPath())
                            {
                                path.AddEllipse(5, 5, width, height);
                                g.SetClip(path);

                                // 绘制图片到圆形路径上
                                g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, width + 5, height + 5));
                            }
                        }

                        // 保存或显示新的圆形图像
                        bmp.Save(@"print/img.png", ImageFormat.Png);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 打印图片保存
        /// </summary>
        /// <param name="ImageData"></param>
        /// <param name="imageName"></param>
        private static void SaveImage(byte[] ImageData, string imageName)
        {
            try
            {
                //保存图片到本地文件夹
                System.IO.MemoryStream ms = new System.IO.MemoryStream(ImageData);
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                //保存到磁盘文件
                string imagePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"print";
                if (!System.IO.Directory.Exists(imagePath))
                    System.IO.Directory.CreateDirectory(imagePath);
                img.Save(System.IO.Path.Combine(imagePath, imageName), System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Dispose();
            }
            catch (Exception exception)
            { };
        }
        public static byte[] GetUrlToBase(string url)
        {
#pragma warning disable CS0168 // 声明了变量，但从未使用过
            try
            {
                var base64String = "";
                HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                string base64sstr = string.Empty;
                using (Stream stream = webResponse.GetResponseStream())
                {
                    var mStream = new MemoryStream();

                    byte[] buffer = new byte[1024];
                    int byteCount;
                    do
                    {
                        byteCount = stream.Read(buffer, 0, buffer.Length);
                        mStream.Write(buffer, 0, byteCount);
                    } while (byteCount > 0);

                    mStream.Seek(0, SeekOrigin.Begin);
                    base64String = Convert.ToBase64String(mStream.ToArray());
                }
                //return base64String;
                WebClient mywebclient = new WebClient();
                byte[] Bytes = mywebclient.DownloadData(url);
                //string Base64string = Convert.ToBase64String(Bytes);
                return Bytes;
            }
            catch (Exception e)
            {
                return new byte[0];
            }
#pragma warning restore CS0168 // 声明了变量，但从未使用过
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.url_start = porPortInfo.url_start;
                    IPorPortInfo.Instance.DeviceId = porPortInfo.DeviceId;
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                    IPorPortInfo.Instance.CoffeePortName = porPortInfo.CoffeePortName;
                    IPorPortInfo.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    IPorPortInfo.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    IPorPortInfo.Instance.SugarInit3 = porPortInfo.SugarInit3;
                    IPorPortInfo.Instance.SugarInit4 = porPortInfo.SugarInit4;
                    IPorPortInfo.Instance.SugarInit5 = porPortInfo.SugarInit5;
                    IPorPortInfo.Instance.SugarInit6 = porPortInfo.SugarInit6;
                    WuLiaoCoffeeSel.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    WuLiaoCoffeeSel.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    WuLiaoCoffeeSel.Instance.SugarInit3 = porPortInfo.SugarInit3;
                }
                if (!Directory.Exists(WarnPath))
                {
                    Directory.CreateDirectory(WarnPath);
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                WarnPath = WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                if (!Directory.Exists(BreakPath))
                {
                    Directory.CreateDirectory(BreakPath);
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                BreakPath = BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                Thread thr3 = new Thread(SocketClients.Instance.socketclient);
                thr3.IsBackground = true;
                thr3.Start();
                Thread thr2 = new Thread(DYCX);
                thr2.IsBackground = true;
                thr2.Start();
                //Thread thread = new Thread(EWMMsg.Instance.IOSerial_Thr);
                //thread.IsBackground = true;
                //thread.Start();
            }
            catch (Exception)
            {
            }
        }
    }
}
